package fanfan.rabbitmq.control;

import com.alibaba.fastjson.JSON;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static java.lang.System.currentTimeMillis;

@RequestMapping("/dcl")
@RestController
public class RabbitController {
    @Autowired
    private AmqpTemplate rabbitTemplate;

     public static HashMap hashMap =new HashMap<Long,String>();

    //发送消息
    @RequestMapping("/send")
    public ModelAndView distributeGo() {
        System.out.println("订单信息 ：order:{}");
        Map map =new HashMap<>();
        long l = currentTimeMillis();
        map.put(l,"请配送至xxxxxxxx ，请在"+l+"秒内接受任务");
        rabbitTemplate.convertAndSend("ex.go", "go", map);
        ModelAndView mav =new ModelAndView("pay");
        return mav;


    }



    //消費消息
    @RequestMapping("/consumer")
    public String getAccumulatedTask() {
        Object o = rabbitTemplate.receiveAndConvert("q.go");

        if(o==null){
            boolean flag=true;
            while(flag) {
                Object object = rabbitTemplate.receiveAndConvert("q.go.dlx");
            if(object==null){
                flag=false;
                break;
            }
                HashMap hashmap = JSON.parseObject(JSON.toJSONString(object), HashMap.class);
            Object next = hashmap.keySet().iterator().next();
            hashMap.put(next, hashmap.get(next));
            }
            return String.valueOf(hashMap);
        }else{
            return "支付成功";
        }

    }
}
